'''
单例装饰器
'''


def singleton(cls):
    # TODO 实现一个单例装饰器
    _instance = {}

    def _singleton(*args, **kargs):
        if cls not in _instance:
            _instance[cls] = cls(*args, **kargs)
        return _instance[cls]

    return _singleton


@singleton
class Test:
    x = 1

    def __init__(self, x=0):
        self.x = x


test1 = Test(1)
test2 = Test(2)

assert test1 is test2, "不是单例"

# 附加题：如何让装饰前后 Test 的 str、type 返回值一致
print(Test)  # <class '__main__.Test'>
print(type(Test))  # <class 'type'>
